from common.mysql_operate import db

class Blog_store():
    def list(self,page):
        sql = "SELECT * FROM blog LIMIT {} ,{}".format((page-1)*5, 5)
        data = db.select_db(sql)
        return data

    def getCount(self):
        sql = "SELECT COUNT(*) as count FROM blog "
        return db.select_db(sql)

    def queryById(self,id):
        sql = "SELECT * FROM blog WHERE id = {}".format(id)
        return db.select_db(sql)

    def getCommentIndex(self):
        sql = "SELECT `desc` FROM blog   order by RAND() LIMIT 6"

        return db.select_db(sql)

    def getTheme(self):
        sql = "SELECT DISTINCT theme  FROM `blog`"
        return db.select_db(sql)

    def addBlog(self, blog):
        sql = "INSERT INTO blog VALUES(NULL,'{}','{}', '{}','{}','{}','{}')".format(blog.title, blog.created_time, blog.theme,
                                                                        blog.desc, blog.authorId, blog.imgUrl)
        return db.execute_db(sql)

    def deleteBlog(self,id):
        sql = "delete FROM blog WHERE id = {}".format(id)
        return db.execute_db(sql)

    def updateById(self,blog):
        sql = "UPDATE blog SET title = '{}',theme='{}',`desc`='{}',img_url='{}' WHERE id = {} AND authorId = {}".format(
            blog.title, blog.theme, blog.desc, blog.imgUrl, blog.id, blog.authorId
        )
        return db.execute_db(sql)


